package com.insis.registry;

import com.insis.loadBalance.LoadBalance;
import com.insis.loadBalance.RoundRobinRule;
import com.alibaba.nacos.api.naming.pojo.Instance;

import java.net.InetSocketAddress;
import java.util.List;

public class NacosServerDiscover {

    private LoadBalance loadBalance;

    public NacosServerDiscover(LoadBalance loadBalance){
        this.loadBalance = (loadBalance == null ? new RoundRobinRule() : loadBalance);
    }


    /**
     * 根据负载均衡返回一个对应服务的实例
     * @param name 服务名称
     * @return 服务地址
     */
    public InetSocketAddress getInstance(String name){
        List<Instance> allInstance = com.insis.registry.NacosUtils.getAllInstance(name);
        Instance instance = allInstance.get(0);
        return new InetSocketAddress(instance.getIp(), instance.getPort());
    }
}
